package net.qhzw.vip.qiyu.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;
import net.qhzw.vip.qiyu.entity.Product;
import net.qhzw.vip.qiyu.entity.Sku;
import net.qhzw.vip.qiyu.param.ProductParam;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.BasketService;
import net.qhzw.vip.qiyu.service.ProductService;
import net.qhzw.vip.qiyu.service.SkuService;
import net.qhzw.vip.qiyu.util.Json;
import net.qhzw.vip.qiyu.util.PageParam;
import net.qhzw.vip.qiyu.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;


/**
 * 商品审核
 */
@RestController
@RequestMapping("/toExamine/prod")
@Slf4j
public class ToExamineProductController {

    @Autowired
    private ProductService productService;


    @Autowired
    private MapperFacade mapperFacade;

    @Autowired
    private SkuService skuService;

    @Autowired
    private BasketService basketService;




    /**
     * 查询上架商品
     */
    @GetMapping("/page")
    public JsonResult<IPage<Product>> page(PageParam<Product> page, ProductParam productParam) {
        IPage<Product> products = productService.listingProducts(page, productParam);
        return JsonResult.ok(products);
    }

    /**
     * 商品审核
     *
     * @param productAudit
     * @return
     */
    /**
     * 修改
     */
    @PostMapping("/audit")
    public JsonResult<String> update(@Valid @RequestBody ProductParam productParam) {
        Product dbProduct = productService.getProductByProdId(productParam.getProdId());
        List<Sku> dbSkus = skuService.list(new LambdaQueryWrapper<Sku>().eq(Sku::getProdId, dbProduct.getProdId()));
        Product product = mapperFacade.map(productParam, Product.class);
        product.setDeliveryMode(Json.toJsonString(productParam.getDeliveryModeVo()));
        product.setUpdateTime(new Date());
        product.setVipPlatformFee(productParam.getSkuList().get(0).getVipPlatformFee());
        product.setSiteFee(productParam.getSkuList().get(0).getSiteFee());
        product.setRevId(SecurityUtils.getUserId());
        dbProduct.setSkuList(dbSkus);
        productService.updateProduct(product, dbProduct);
        List<String> userIds = basketService.listUserIdByProdId(product.getProdId());
        for (String userId : userIds) {
            basketService.removeShopCartItemsCacheByUserId(userId);
        }
        for (Sku sku : dbSkus) {
            skuService.removeSkuCacheBySkuId(sku.getSkuId(), sku.getProdId());
        }
        return JsonResult.ok();
    }



//    @PostMapping("/audit")
//    public JsonResult audit(@RequestBody ProductAudit productAudit) {
//        log.info("商品审核: {}", JSON.toJSONString(productAudit, true));
//        productAudit.setOperator(SecurityUtils.getLoginUser().getUserId());
//        if (productAudit.getSkuList() != null) {
//            List<Sku> skuList = productAudit.getSkuList();
//            for (Sku sku : skuList) {
//                sku.setStatus(productAudit.getStatus());
//            }
//        }
//        boolean isOK = productService.productAudit(productAudit);
//        return JsonResult.ok();
//    }

}
